package ltd.bugs.cute.mybatis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ltd.bugs.cute.cache.CacheClient;
import ltd.bugs.cute.cache.CacheKeyUtil;
import ltd.bugs.cute.cache.callback.GetCallback;
import ltd.bugs.cute.mybatis.dao.StudentDAO;
import ltd.bugs.cute.mybatis.model.Student;

@Service
public class StudentService {

  @Autowired
  private StudentDAO studentDAO;
  private CacheKeyUtil cacheKeyUtil = new CacheKeyUtil(StudentService.class);

  public void add(Student student) {
    studentDAO.add(student);
  }

  public void update(Student student) {
    studentDAO.update(student);
  }

  public Student get(long id) {
    return CacheClient.get(cacheKeyUtil.getRecKey(".id." + id), new GetCallback<Student>() {

      @Override
      public String getCacheGroup() {
        return null;
      }

      @Override
      public Student getObject() {
        return studentDAO.get(id);
      }
    });
  }

  public void delete(long id) {
    studentDAO.delete(id);
  }
}
